using System;
using System.IO;

namespace ReadWriteCsv
{
	public class CsvFileReader : StreamReader
	{
		public CsvFileReader(Stream stream) : base(stream)
		{
		}

		public CsvFileReader(string filename) : base(filename)
		{
		}

		public bool ReadRow(CsvRow row)
		{
			row.LineText = this.ReadLine();
			if (string.IsNullOrEmpty(row.LineText))
			{
				return false;
			}
			row.LineText = row.LineText.Replace("\\n", "\n");
			int i = 0;
			int num = 0;
			while (i < row.LineText.Length)
			{
				string text;
				if (row.LineText[i] == '"')
				{
					i++;
					int num2 = i;
					while (i < row.LineText.Length)
					{
						if (row.LineText[i] == '"')
						{
							i++;
							if (i >= row.LineText.Length || row.LineText[i] != '"')
							{
								i--;
								break;
							}
						}
						i++;
					}
					text = row.LineText.Substring(num2, i - num2);
					text = text.Replace("\"\"", "\"");
				}
				else
				{
					int num3 = i;
					while (i < row.LineText.Length && row.LineText[i] != ',')
					{
						i++;
					}
					text = row.LineText.Substring(num3, i - num3);
				}
				if (num < row.Count)
				{
					row[num] = text;
				}
				else
				{
					row.Add(text);
				}
				num++;
				while (i < row.LineText.Length && row.LineText[i] != ',')
				{
					i++;
				}
				if (i < row.LineText.Length)
				{
					i++;
				}
			}
			while (row.Count > num)
			{
				row.RemoveAt(num);
			}
			return row.Count > 0;
		}
	}
}
